package com.mobile.challenge.ui.report.unit.manager.daily

import android.graphics.Color
import android.os.Bundle
import androidx.lifecycle.Observer
import com.github.mikephil.charting.animation.Easing
import com.github.mikephil.charting.components.Legend
import com.mobile.challenge.R
import com.mobile.challenge.databinding.FragmentManagerDailyReportBinding
import com.mobile.challenge.ui.report.adapter.IRefresh
import com.mobile.frame.mvvmframe.base.BaseFragment
import java.util.*


/**
 * @Author Touge
 * @Date 2020/9/16 21:24
 * @Description
 */
class ManagerUnitDailyReportFragment : BaseFragment<ManagerUnitDailyReportViewModel, FragmentManagerDailyReportBinding>()
        , IRefresh {

    companion object {
        fun newInstance(): ManagerUnitDailyReportFragment {
            val args = Bundle()

            val fragment = ManagerUnitDailyReportFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_manager_daily_report
    }

    override fun initData(savedInstanceState: Bundle?) {
        initBarChart()
        mViewModel.generateData(Calendar.getInstance())
        mViewModel.pieData.observe(this, Observer { data ->
            mBinding.pieChart.data = data
            mBinding.pieChart.animateXY(1000, 1000)
        })
    }

    private fun initBarChart() {
        // 初始化饼状图属性
        with(mBinding.pieChart) {
            // 显示百分比
            setUsePercentValues(false)
            description.isEnabled = false
            // 设置边距
            setExtraOffsets(40f, 10f, 40f, 5f)
            setDrawCenterText(false)

            // 展开动画
            animateY(1000, Easing.EaseInOutQuad)

            // 图例
            legend.verticalAlignment = Legend.LegendVerticalAlignment.TOP
            legend.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT
            legend.orientation = Legend.LegendOrientation.VERTICAL
            legend.setDrawInside(false)
            legend.xEntrySpace = 7f
            legend.yEntrySpace = 0f
            legend.yOffset = 0f

            // label 文字样式
            setDrawEntryLabels(false)
            setEntryLabelColor(Color.BLACK)
            setEntryLabelTextSize(12f)

            // 关闭内环
            isDrawHoleEnabled = true

            // 关闭旋转
            isRotationEnabled = false
        }
    }

    override fun refresh(calendar: Calendar, period: Int) {
        if (mViewModel != null) {
            mViewModel.generateData(calendar)
        }
    }
}